//
//  CourseInformationViewController.m
//  ClassRoom
//
//  Created by apple on 12-12-22.
//
//

#import "CourseInformationViewController.h"
#import "ASIHTTPRequest.h"
#import "JSONKit.h"
#import "LoginStatus.h"
#import "UIImageView+WebCache.h"
#import "EvaluateViewController.h"

@interface CourseInformationViewController (){
    NSString *telePhontNumber;
}

@end

@implementation CourseInformationViewController
@synthesize backGroundScrollView;
@synthesize nameLabel;
@synthesize timeLabel;
@synthesize priceLabel;
@synthesize phoneLabel;
@synthesize addressLabel;
@synthesize discountLabel;
@synthesize informationLabel;
@synthesize phoneImageView;
@synthesize addressImageView;
@synthesize discountImageView;
@synthesize informationImageView;
@synthesize courseImageView;
@synthesize evalauteButton;
@synthesize collectButton;
@synthesize courseInformationDictionary;
@synthesize imageURL = _imageURL;
@synthesize phone;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"课程详情";
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    courseInformationDictionary = [[NSDictionary alloc] init];
    [self SetUpView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)SetUpView{
    backGroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    backGroundScrollView.backgroundColor = [UIColor clearColor];
    backGroundScrollView.delegate = self;
    backGroundScrollView.bounces = YES;
    backGroundScrollView.showsHorizontalScrollIndicator = NO;
    backGroundScrollView.showsVerticalScrollIndicator = YES;
    [self.view addSubview:backGroundScrollView];
    
    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    nameLabel.numberOfLines = 0;
    nameLabel.textAlignment = UITextAlignmentCenter;
    [backGroundScrollView addSubview:nameLabel];
    
    courseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 46, 90, 100)];
    courseImageView.backgroundColor = [UIColor blackColor];
    [backGroundScrollView addSubview:courseImageView];
    
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 50, 90, 20)];
    dateLabel.font = [UIFont systemFontOfSize:17];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.text = @"开班日期：";
    [backGroundScrollView addSubview:dateLabel];
    [dateLabel release];
    
    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 50, 90, 20)];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.font = [UIFont systemFontOfSize:17];
    [backGroundScrollView addSubview:timeLabel];
    
    UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(130, 75, 90, 20)];
    priceLbl.backgroundColor = [UIColor clearColor];
    priceLbl.font = [UIFont systemFontOfSize:17];
    priceLbl.text = @"课程价格：";
    [backGroundScrollView addSubview:priceLbl];
    [priceLbl release];
    
    priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 75, 90, 20)];
    priceLabel.backgroundColor = [UIColor clearColor];
    priceLabel.font = [UIFont systemFontOfSize:17];
    [backGroundScrollView addSubview:priceLabel];
    
    discountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 110, 180, 30)];
    discountImageView.backgroundColor = [UIColor redColor];
    [backGroundScrollView addSubview:discountImageView];
    
    evalauteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    evalauteButton.frame = CGRectMake(35, 155, 100, 30);
    evalauteButton.backgroundColor = [UIColor clearColor];
    [evalauteButton setImage:[UIImage imageNamed:@"Course_Evalaute_button"] forState:UIControlStateNormal];
    [evalauteButton addTarget:self action:@selector(pressDescriptionAction) forControlEvents:UIControlEventTouchUpInside];
    [backGroundScrollView addSubview:evalauteButton];
    
    collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    collectButton.frame = CGRectMake(185, 155, 100, 30);
    collectButton.backgroundColor = [UIColor clearColor];
    [collectButton setImage:[UIImage imageNamed:@"Course_Collect_button"] forState:UIControlStateNormal];
    [collectButton addTarget:self action:@selector(pressCollectionAction) forControlEvents:UIControlEventTouchUpInside];
    [backGroundScrollView addSubview:collectButton];
    
    UIImageView *lineOneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 191, 320, 4)];
    lineOneImageView.backgroundColor = [UIColor clearColor];
    lineOneImageView.image = [UIImage imageNamed:@"CourseInfo_line"];
    [backGroundScrollView addSubview:lineOneImageView];
    [lineOneImageView release];
    
    phoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(19, 201, 32, 32)];
    phoneImageView.image = [UIImage imageNamed:@"CourseInfo_Phone_ImageView"];
    [backGroundScrollView addSubview:phoneImageView];
    [phoneImageView release];
    
    UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    phoneButton.frame = CGRectMake(0, 195, 320, 50);
    phoneButton.backgroundColor = [UIColor clearColor];
    [phoneButton addTarget:self action:@selector(callTheTelePhoneNumber) forControlEvents:UIControlEventTouchUpInside];
    [backGroundScrollView addSubview:phoneButton];
    
    phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(61, 205, 200, 30)];
    phoneLabel.backgroundColor = [UIColor clearColor];
    phoneLabel.font = [UIFont systemFontOfSize:15];
    [backGroundScrollView addSubview:phoneLabel];
    
    UIImageView *arrowhead = [[UIImageView alloc] initWithFrame:CGRectMake(290, 210, 13, 20)];
    arrowhead.backgroundColor = [UIColor clearColor];
    arrowhead.image = [UIImage imageNamed:@"Arrowhead_Green"];
    [backGroundScrollView addSubview:arrowhead];
    
    UIImageView *lineTwoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 241, 320, 4)];
    lineTwoImageView.backgroundColor = [UIColor clearColor];
    lineTwoImageView.image = [UIImage imageNamed:@"CourseInfo_line"];
    [backGroundScrollView addSubview:lineTwoImageView];
    [lineTwoImageView release];
    
    addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(19, 250, 32, 32)];
    addressImageView.image = [UIImage imageNamed:@"CourseInfo_Address_ImageView"];
    [backGroundScrollView addSubview:addressImageView];
    [addressImageView release];
    
    addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(61, 252, 259, 30)];
    addressLabel.backgroundColor = [UIColor clearColor];
    addressLabel.font = [UIFont systemFontOfSize:15];
    [backGroundScrollView addSubview:addressLabel];
    
    UIImageView *lineThreeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 288, 320, 4)];
    lineThreeImageView.backgroundColor = [UIColor clearColor];
    lineThreeImageView.image = [UIImage imageNamed:@"CourseInfo_line"];
    [backGroundScrollView addSubview:lineThreeImageView];
    [lineThreeImageView release];
    
    discountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 297, 34, 34)];
    discountImageView.image = [UIImage imageNamed:@"CourseInfo_DisCount_ImageView"];
    [backGroundScrollView addSubview:discountImageView];
    [discountImageView release];
    
    discountLabel = [[UILabel alloc] initWithFrame:CGRectMake(61, 299, 259, 30)];
    discountLabel.backgroundColor = [UIColor clearColor];
    discountLabel.text = @"暂无优惠";
    discountLabel.font = [UIFont systemFontOfSize:15];
    [backGroundScrollView addSubview:discountLabel];
    
    UIImageView *lineFourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 335, 320, 4)];
    lineFourImageView.backgroundColor = [UIColor clearColor];
    lineFourImageView.image = [UIImage imageNamed:@"CourseInfo_line"];
    [backGroundScrollView addSubview:lineFourImageView];
    [lineFourImageView release];
    
    informationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 343, 34, 34)];
    informationImageView.image = [UIImage imageNamed:@"CourseInfo_Information_ImageView"];
    [backGroundScrollView addSubview:informationImageView];
    [informationImageView release];
    
    informationLabel = [[UILabel alloc] initWithFrame:CGRectMake(61, 345, 250, 30)];
    informationLabel.backgroundColor = [UIColor clearColor];
    informationLabel.font = [UIFont systemFontOfSize:15];
    [backGroundScrollView addSubview:informationLabel];
}

- (void)SetSomeChanges:(NSDictionary *)dictionary buttonEnable:(BOOL)bbool{
    NSLog(@"dictionary = %@",dictionary);
    if (nil != [dictionary objectForKey:@"courseName"]) {
        nameLabel.text = [dictionary objectForKey:@"courseName"];
    }
    if (nil != [dictionary objectForKey:@"logourl"]) {
        [self configureView];
    }
    if (nil != [dictionary objectForKey:@"startTime"]) {
        timeLabel.text = [dictionary objectForKey:@"startTime"];
    }
    if (nil != [dictionary objectForKey:@"price"]) {
        priceLabel.text = [NSString stringWithFormat:@"%@ 元",[dictionary objectForKey:@"price"]];
    }
    if (nil != [dictionary objectForKey:@"telePhone"]) {
        phoneLabel.text = [dictionary objectForKey:@"telePhone"];
        telePhontNumber = [dictionary objectForKey:@"telePhone"];
    }
    if (nil != [dictionary objectForKey:@"address"]) {
        addressLabel.text = [dictionary objectForKey:@"address"];
    }
    
    float scrollViewHight = 0.0;
    
    if (nil != [dictionary objectForKey:@"description"]) {
        //自适应高度
        UIFont *font = [UIFont systemFontOfSize:15];
        informationLabel.lineBreakMode = UILineBreakModeWordWrap;
        informationLabel.numberOfLines = 0;
        NSString *str = @"苹果5 已经问世，可悲的是美国的苹果5手机自带的地图里面，把钓鱼岛划给了日本，请大家相互转告，不要再买苹果5的智能手机，不要做了错事，让美国人耻笑我们中国人，中国人一定要团结起来！必须转。";
//        CGSize titleSize = [(NSString *)[dictionary objectForKey:@"description"] sizeWithFont:font constrainedToSize:CGSizeMake(informationLabel.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
        CGSize titleSize = [str sizeWithFont:font constrainedToSize:CGSizeMake(informationLabel.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
        informationLabel.frame = CGRectMake(61, 345, 250, titleSize.height);
//        informationLabel.text = [dictionary objectForKey:@"description"];
        informationLabel.text = str;
        scrollViewHight = titleSize.height + 345.0;
    }
    CGSize newBackGroundScrollViewSize = CGSizeMake(320, scrollViewHight);
    [backGroundScrollView setContentSize:newBackGroundScrollViewSize];
    
    courseInformationDictionary = dictionary;
    
    if ([LoginStatus sharedLoginStatus].ifLogined=TRUE) {
        if (!bbool) {
            evalauteButton.enabled = NO;
            collectButton.enabled = NO;
        }
    }else {
        evalauteButton.enabled = NO;
        collectButton.enabled = NO;
    }
    
}

#pragma mark - Managing the detail item

- (void)setImageURL:(NSURL *)imageURL
{
    if (_imageURL != imageURL)
    {
        _imageURL = imageURL;
        [self configureView];
    }
}
- (void)configureView
{
    if (self.imageURL)
    {
        [self.courseImageView setImageWithURL:self.imageURL placeholderImage:nil options:SDWebImageProgressiveDownload];
    }
}


#pragma mark - 
#pragma mark UIActionSheet   telephone

- (void)callTheTelePhoneNumber{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"拨打电话" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:telePhontNumber otherButtonTitles:nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telePhontNumber]];
    }
}
/*
#pragma mark -
#pragma mark ASIHTTPRequest 

- (void)requestFinished:(ASIHTTPRequest *)request{
    NSLog(@"%@",request);
    UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
    NSLog(@"\n\n[request downloadDestinationPath] =\n\n %@\n\n",[request downloadDestinationPath]);
    if (img) {
        NSLog(@"image not null");
        [courseImageView setImage:img];
        
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    NSError *error = [request error];
    if (error) {
        if([[request error] domain] !=NetworkRequestErrorDomain || [[request error]code] !=ASIRequestCancelledErrorType) {
            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"DownLoad Failed" message:@"DownLoad image failed" delegate:nil cancelButtonTitle:@"ok"otherButtonTitles:nil] autorelease];
            [alert show];
        }
    }
}
*/
#pragma mark -
#pragma mark description 、collection action

- (void)pressDescriptionAction{
    NSLog(@"评价");
    NSLog(@"courseInformationDictionary  = %@",courseInformationDictionary);
    EvaluateViewController *evaluateViewcontroller=[[[EvaluateViewController alloc]init] autorelease];
    evaluateViewcontroller.courseInfoDic=[[NSMutableDictionary alloc]init];
    evaluateViewcontroller.courseInfoDic=(NSMutableDictionary *)courseInformationDictionary;
    [self.navigationController pushViewController:evaluateViewcontroller animated:YES];

}

- (void)pressCollectionAction{
    NSLog(@"收藏");
}

@end
